package com.qf.cm.controller;

import com.qf.cm.pojo.Project;
import com.qf.cm.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Description: 公用常量
 * @Data: 2022/9/7 11:52
 * @Pacakge: com.qf.cm.controller
 * @ClassName: ProjectController
 * @Author: fan
 */
@Controller
@RequestMapping("project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @RequestMapping("findAllProject")
    public ModelAndView findAllProject(){
        ModelAndView modelAndView = new ModelAndView();
        List<Project> projects = projectService.findAllProject();
        modelAndView.setViewName("/pages/project/list");
        modelAndView.addObject("list", projects);
        return modelAndView;
    }
    @RequestMapping("findProjectByPid")
    public ModelAndView findProjectByPid(Integer pid){
        ModelAndView modelAndView = new ModelAndView();
        Project project = projectService.findProjectById(pid);
        modelAndView.addObject("project",project);
        modelAndView.setViewName("/pages/project/update");
        return modelAndView;
    }
    @PostMapping("updateProject")
    public String updateProject(Project project) {
        System.out.println("project = " + project);
        int i = projectService.updateProject(project);
        if (i > 0) {
            return "forward:/project/findAllProject";
        } else {
            return "redirect:/index";
        }
    }
    @RequestMapping("findAll")
    public ModelAndView findAll(){
        ModelAndView modelAndView = new ModelAndView();
        List<Project> projects = projectService.findAll();
        System.out.println("projects = " + projects);
        modelAndView.setViewName("/pages/project/all");
        modelAndView.addObject("projects",projects);
        return modelAndView;
    }
    @RequestMapping("findProjectAndStages")
    public ModelAndView findProjectAndStages(){
        ModelAndView modelAndView = new ModelAndView();
        List<Project> projectAndStages = projectService.findProjectAndStages();
        modelAndView.addObject("list",projectAndStages);
        return modelAndView;
    }
}
